package com.jkl.comm.service;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.xml.namespace.QName;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAP11Constants;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 * <p>
 * 调用webservic工具类
 * <p>
 * <p>
 * 〈功能详细描述〉
 * <p>
 * <p>
 * 变更说明：neusoft 2018年4月26日 〈功能变更说明第一行〉
 * <p>
 * Date:2018年4月26日
 * 
 * @author ji.k
 * @version 1.0
 */

public class CallWebServiceUtil {

	public static void main(String[] args) throws Exception {
		String url = "http://115.29.2.80/JianCheWebService/OutJkyService.asmx";
		String nameSpace = "http://tempuri.org/";
		String methodName = "OutSGXKinfo";
		String action = "http://tempuri.org/OutSGXKinfo";
		Map<String, String> map = new HashMap<>();
		map.put("username", "Jkyser11e8-8ee6-55d11908dc3a");
		map.put("userpssword", "9f4b4df7-ed38-11e8-8ee6-55d11908dc3a");
//		map.put("username", "Jky_SGXK！@#=user");
//		map.put("userpssword", "Jky_SGXK！@#=Pwd");
		map.put("BUILDERLICENCENUM", "330681201707190101");
		String result = callWebServiceByDocument(url, nameSpace, methodName, action, map);
		System.out.println(result);
		Document doc = DocumentHelper.parseText(result);
		String result2 = doc.getRootElement().element("OutSGXKinfoResult").getStringValue();
		Document doc2 = DocumentHelper.parseText(result2);
		Element ele = doc2.getRootElement().element("sgxkinfo");
		System.out.println(ele.asXML());
	}

	/**
	 * Description: 通过document方式调用
	 *
	 * @param url
	 * @param nameSpace
	 * @param methodName
	 * @param action
	 * @param params
	 *            自定义参数,key值对应wsdl中的参数名
	 * @return
	 * @throws AxisFault
	 * @author ji.k
	 */

	public static String callWebServiceByDocument(String url, String nameSpace, String methodName, String action,
			Map<String, String> params) throws AxisFault {

		ServiceClient serviceClient = new ServiceClient();
		Options option = new Options();
		option.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
		option.setAction(action);

		EndpointReference epfs = new EndpointReference(url);
		option.setTo(epfs);
		serviceClient.setOptions(option);

		OMFactory fac = OMAbstractFactory.getOMFactory();
		OMNamespace namespace = fac.createOMNamespace(nameSpace, "");
		OMElement element = fac.createOMElement(methodName, namespace);
		Set<String> keys = params.keySet();
		keys.stream().forEach(e -> {
			OMElement theCityCode = fac.createOMElement(e, namespace);
			theCityCode.setText(params.get(e));
			element.addChild(theCityCode);
		});
		OMElement result = serviceClient.sendReceive(element);
		serviceClient.cleanupTransport();
		return result.toString();
	}

	/**
	 * Description: 通过RPC方式调用,参数默认为arg0,arg1
	 *
	 * @param url
	 * @param nameSpace
	 * @param methodName
	 * @param action
	 * @param args
	 * @return
	 * @throws Exception
	 * @author ji.k
	 */

	public static String callWebServiceByRPC(String url, String nameSpace, String methodName, String action,
			Object[] args) throws Exception {

		// 使用RPC方式调用WebService
		RPCServiceClient serviceClient = new RPCServiceClient();
		// 指定调用WebService的URL
		EndpointReference targetEPR = new EndpointReference(url);
		Options options = serviceClient.getOptions();
		// 确定目标服务地址
		options.setTo(targetEPR);
		if (StringUtils.isNotEmpty(action)) {
			options.setAction(action);
		}
		QName qname = new QName(nameSpace, methodName);
		OMElement element = serviceClient.invokeBlocking(qname, args);
		// 返回结果就是一段由OMElement对象封装的xml字符串。
		String result = element.getFirstElement().getText();
		return result;
	}
}
